#ifndef __CALCULATOR_HPP__
#define __CALCULATOR_HPP__

#include "protocal.hpp"
#include "package_unpack.hpp"

    

class Calculator
{
    Response CalculatorHelper(const Request& rep)
    {
        int result = 0 ;
        int node = Right;       // 用1表示计算结果正确
        int first = rep.Get_first() , second = rep.Get_second();
        switch (rep.Get_op())
        {
            case '+':
            {
                result = first + second;
                break;
            }
            case '-':
            {
                result = first - second;
                break;
            }
            case '*':
            {
                result = first * second;
                break;
            }
            case '/':
            {
                if(second == 0) node = Division_Zero;
                else result = first / second;
                break;
            }
            case '%':
            {
                if(second == 0) node = Mode_Zero;
                else result = first % second;
                break;
            }
            default:
                node = No_Operaotr;
            break;
        }
        return Response(result , node);
    }

public:
    std::string operator()(std::string &package)
    {
        // 1.解包
        // 2.将字符串转Request
        // 3.计算Request
        // 4.将计算结果转为Response
        // 5.序列化Response + 加报头

        std::string content ;
        if(Decode(package , content) == false) return "" ; // 表示没有完整的报文,不需要进行计算

        // 此时content中是有效载荷,进行反序列化
        Request req(content);
        // 进行计算
        Response res = CalculatorHelper(req);

        return Encode(res.Serialize());
    }
};


#endif